package com.authine.cloudpivot.ext.workflowevent;


import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.dto.model.invoice.InvoiceApprove;
import com.authine.cloudpivot.ext.service.invoice.InvoiceApplyService;
import com.authine.cloudpivot.ext.service.invoice.impl.InvoiceApplyServiceImpl;
import com.authine.cloudpivot.ext.service.settle.SettleService;
import com.authine.cloudpivot.ext.service.settle.impl.SettleServiceImpl;
import com.authine.cloudpivot.ext.utils.BigDecimalUtils;
import com.authine.cloudpivot.ext.utils.FinancialCalculationUtils;
import com.authine.cloudpivot.ext.utils.JdbcTemplateUtils;
import com.authine.hermes.app.flow.engine.delegate.event.EventType;
import com.authine.hermes.app.flow.engine.delegate.event.WorkflowEvent;
import com.authine.hermes.app.flow.engine.delegate.event.impl.FlowActivityEventImpl;
import com.authine.hermes.app.flow.engine.delegate.event.impl.WorkItemEventImpl;
import com.authine.hermes.app.flow.engine.impl.persistence.entity.WorkItemEntity;
import com.authine.hermes.app.flow.runtime.engine.listener.AbstractWorkflowEventListener;
import com.authine.mvp.app.launcher.domain.utills.ApplicationContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@Slf4j
//@Component(value="MyWorkflowEventListener")
public class MyWorkflowEventListener extends AbstractWorkflowEventListener {

    private final JdbcTemplate jdbcTemplate = ApplicationContextUtils.getBean(JdbcTemplate.class);
    public static SettleService settleService = new SettleServiceImpl();
    public static InvoiceApplyService invoiceApplyService = new InvoiceApplyServiceImpl();

    @Override
    public void onEvent(WorkflowEvent workflowEvent) {
        log.info("----------------------------------------------------");
        log.info("流程code:{},流程id:{},variables:{}",workflowEvent.getWorkflowCode(),workflowEvent.getWorkflowInstanceId(),workflowEvent.getVariables());
        log.info("----------------------------------------------------");
        EventType eventType = workflowEvent.getType();
        log.info("----------eventType------{}",eventType.getDesc());
        if(EventType.WF_START == eventType) {
            log.info("####################################################");
            log.info("####### 1. 流程开始了");
        }

        if(EventType.WF_END == eventType) {
            log.info("####################################################WF_END流程结束处理");
            log.info("####### 2. 流程结束了");
            //业务id获取，对应t_settle表id
            String bizObjectId=this.getFlowInstanceEntityById(workflowEvent.getWorkflowInstanceId()).getBizObjectId();
            if ("purchasePaymentApprov".equals(workflowEvent.getWorkflowCode())){
                //TODO 采购付款审批 最终结束后事件逻辑
            }
        }

        if(EventType.WF_ABORT == eventType) {
            log.info("####################################################");
            log.info("####### 3. 流程作废了");
        }

        if(EventType.WF_ACTIVATE == eventType) {
            log.info("####################################################");
            log.info("####### 4. 流程激活后");
        }

        if(EventType.ACTIVITY_ACTIVATED == eventType) {
            FlowActivityEventImpl activityEvent = (FlowActivityEventImpl) workflowEvent;
            log.info("####################################################");
            log.info("####### 5. 活动激活后,活动code：{}",activityEvent.getActivityCode());
        }

        if(EventType.ACTIVITY_ACTIVATING == eventType) {
            FlowActivityEventImpl activityEvent = (FlowActivityEventImpl) workflowEvent;
            log.info("####################################################");
            log.info("####### 6. 活动激活前,活动code：{}",activityEvent.getActivityCode());
        }

        if(EventType.ACTIVITY_FINISHED == eventType) {
            FlowActivityEventImpl workItemEntity = (FlowActivityEventImpl) workflowEvent;
            log.info("####################################################活动完成了::活动code{}",workItemEntity.getActivityCode());
            log.info("####### 7. 活动完成了,活动code：{}", workItemEntity.getActivityCode());
            //业务id获取，对应t_settle表id
            String bizObjectId=this.getFlowInstanceEntityById(workflowEvent.getWorkflowInstanceId()).getBizObjectId();
            if ("purchasePaymentApprov".equals(workflowEvent.getWorkflowCode())&&"Activity3".equals(workItemEntity.getActivityCode())){
                //业务审批通过不做任何处理
            }
            if ("purchasePaymentApprov".equals(workflowEvent.getWorkflowCode())&&"Activity6".equals(workItemEntity.getActivityCode())){
                log.info("结算单{}财务审批通过！！！", bizObjectId);
                settleService.approveAgree(Collections.singletonList(bizObjectId));
            }
            if ("invoice_approve".equals(workflowEvent.getWorkflowCode())&&"Activity3".equals(workItemEntity.getActivityCode())){
                //开票审批 通过事件业务逻辑
                log.info("发票申请单{}审批通过！！！", bizObjectId);
                InvoiceApprove approve = new InvoiceApprove();
                approve.setId(bizObjectId).setStatus("DrawerConfirming").setApprovalReason("");
                invoiceApplyService.approveInvoice(approve);
            }
        }

        if(EventType.ACTIVITY_CANCELED == eventType) {
            FlowActivityEventImpl activityEvent = (FlowActivityEventImpl) workflowEvent;
            log.info("####################################################");
            log.info("####### 8. 活动取消了,活动code：{}",activityEvent.getActivityCode());
        }

        if(EventType.ENTITY_CREATED == eventType) {
            log.info("####################################################");
            log.info("####### 9. 创建了1个待办任务");
        }

        if(EventType.ENTITY_UPDATED == eventType) {
            log.info("####################################################修改实体");
            log.info("####### 10. 更新了1个待办任务");
        }

        if(EventType.ENTITY_DELETED == eventType) {
            log.info("####################################################");
            log.info("####### 11. 删除了1个待办任务");
        }

        if(EventType.WORKITEM_SUBMITTED == eventType) {
            //未打印日志
            WorkItemEventImpl workItemEvent = (WorkItemEventImpl) workflowEvent;
            log.info("####################################################");
            WorkItemEntity workItemEntity = workItemEvent.getWorkItemEntity();
            log.info("####### 12. 任务提交后,活动code：{},处理人:{},BOId:{}",workItemEntity.getActivityCode(),
                    workItemEntity.getParticipant()+"-"+workItemEntity.getParticipantName(),
                    this.getFlowInstanceEntityById(workItemEvent.getWorkflowInstanceId()).getBizObjectId());
            log.info("*************************************************");

        }

        if(EventType.WORKITEM_REJECTED == eventType) {
            FlowActivityEventImpl workItemEvent = (FlowActivityEventImpl) workflowEvent;
//            WorkItemEventImpl workItemEvent = (WorkItemEventImpl) workflowEvent;
            log.info("####################################################");
            log.info("####### 13. 任务驳回后,活动code：{}",workItemEvent.getActivityCode());
            //业务id获取，对应t_settle表id
            String bizObjectId=this.getFlowInstanceEntityById(workflowEvent.getWorkflowInstanceId()).getBizObjectId();
            if ("purchasePaymentApprov".equals(workflowEvent.getWorkflowCode())&&"Activity3".equals(workItemEvent.getActivityCode())){
                //采购付款审批 业务审批拒绝事件业务逻辑
                log.info("结算单{}业务审批拒绝！！！", bizObjectId);
                settleService.close(Collections.singletonList(bizObjectId));
            }
            if ("purchasePaymentApprov".equals(workflowEvent.getWorkflowCode())&&"Activity6".equals(workItemEvent.getActivityCode())){
                //采购付款审批 财务审批拒绝事件业务逻辑
                log.info("结算单{}财务审批拒绝！！！", bizObjectId);
                settleService.close(Collections.singletonList(bizObjectId));
            }
            if ("invoice_approve".equals(workflowEvent.getWorkflowCode())&&"Activity3".equals(workItemEvent.getActivityCode())){
                //开票审批 拒绝事件业务逻辑
                log.info("发票申请单{}审批拒绝！！！", bizObjectId);
                InvoiceApprove approve = new InvoiceApprove();
                approve.setId(bizObjectId).setStatus("Returned").setApprovalReason("");
                invoiceApplyService.approveInvoice(approve);
            }
        }
    }

//    public BigDecimal getPretaxPrice(BigDecimal pretax_price,BigDecimal num,BigDecimal taxRate,BigDecimal taxRateFreight,BigDecimal pretaxFreight){
//
//    }
}
